<template>
	<view class="fenlei">
		<image src="https://shop.hacuu.com/static/Rectangle 21541@2x.png" class="gouwucheback">
		</image>
		<view class="headertop" :style="{'background':flag1?'#FCE5CC':''}">
			<view class="box"
				:style="{'margin-top':statusBarHeight+'px','width':'100%','height':barHeight+'px','display':'flex','align-items': 'center'}">
				<image src="https://39gongli.oss-cn-beijing.aliyuncs.comhttps://shop.hacuu.com/static/Group 2@2x.png" class="pagelefticon"
					mode=""></image>
			</view>
		</view>
		<view class="classify">
			<view class="itembox" @click="qiehuan(-1)">
				<image src="https://shop.hacuu.com/static/Group 11592@2x.png" mode=""></image>
				<view class="text" v-if="active!=-1">
					全部
				</view>
				<view class="xz" v-if="active==-1">
					<image src="https://shop.hacuu.com/static/Union@2x.png" mode=""></image>
					<view class="title">
						全部
					</view>
				</view>
			</view>
			<view class="itembox" v-for="(item,index)  in  classlistarr" :key="item.id" @click="qiehuan(index,item)">
				<image :src="item.image" mode=""></image>
				<view class="text" v-if="active!=index">
					{{item.name}}
				</view>
				<view class="xz" v-if="active==index">
					<image src="https://shop.hacuu.com/static/Union@2x.png"></image>
					<view class="title">
						{{item.name}}
					</view>
				</view>
			</view>
		</view>
		<view class="bottombox">
			<view class="leftmenubox">
				<view :class="active1==-1?'itemxq':'itemxq1'" @click="qiehuan1(-1)">
					<view class="activebox" v-if="active1==-1">

					</view>
					全部分类
				</view>
				<view :class="active1==index?'itemxq':'itemxq1'" v-if="active!=-1" v-for="(item,index) in classlistarr1"
					@click="qiehuan1(index,item)" :key="item.id">
					<view class="activebox" v-if="active1==index">

					</view>
					{{item.name}}
				</view>
			</view>
			<view class="rightbox">
				<view class="classifytitle">
					{{active1==-1?'全部分类':classlistarr1[active1]['name']}}
				</view>
				<view class="msgicon" v-if="!List.length">
					<image src="https://shop.hacuu.com/static/Frame@2x(21).png" mode="widthFix"></image>
					<view class="msgtext">
						暂无商品
					</view>
				</view>
				<view class="itemshangp" v-for="item in List" :key="item.id" @click="gopage(item.id)">
					<image :src="item.image" class="leftimg" mode=""></image>
					<view class="texts">
						<view class="t1">
							{{item.title}}
						</view>
						<view class="t2">
							{{item.subtitle}}
						</view>
						<view class="line">
							<view class="jiage">
								￥{{jigaefun(item.price[0])}}.<span
									style="font-size:20rpx ;">{{jigaefun(item.price[0],'后')}}</span>
							</view>
							<image src="https://shop.hacuu.com/static/Group 11462@2x.png" class="righticon" mode=""></image>
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>
<script setup>
	import {
		ref,
		onMounted,
		reactive
	} from 'vue'
	import {
		classlist,
		goods
	} from '@/api/home.js'
	import {
		onLoad,
		onPageScroll
	} from "@dcloudio/uni-app";
	const barHeight = ref('')
	const statusBarHeight = ref('')
	const classlistarr = ref([])
	const classlistarr1 = ref([])
	const query = reactive({
		page: 1,
		limit: 10,
		keyword: '',
		category_id: '',
	})
	const List = ref([])
	const pid = ref('')
	const total = ref(0)
	const active = ref(-1)
	const active1 = ref(-1)
	onLoad(() => {
		shangpinginit()
	})
	// 商品分类
	async function shangpinginit() {
		const res = await classlist({
			pid: 0
		})
		if (res.code == 1) {
			classlistarr.value = res.data
			shangpingListrinit()
		}
	}

	function qiehuan(index, item) {
		active.value = index
		if (index == -1) {
			pid.value = 0
		} else {
			pid.value = item.id
		}
		shangpinginit1()
	}

	function qiehuan1(index, item) {
		active1.value = index
		if (index == -1) {
			query.category_id = classlistarr.value[active.value]['id']
		} else {
			query.category_id = item.id
		}
		query.page = 1
		List.value = []
		shangpingListrinit()
	}

	async function shangpinginit1() {
		if (pid.value == 0) return
		const res = await classlist({
			'pid': pid.value
		})
		if (res.code == 1) {
			classlistarr1.value = res.data
			qiehuan1(-1)
		}
	}
	async function shangpingListrinit() {
		const res = await goods(query)
		if (res.code == 1) {
			console.log(res, 'res----------------');
			List.value.push(...res.data.data)
			total.value = res.data.total
		}
	}

	function jigaefun(str1, strr) {
		let number = str1
		let str = number.toString(); // 转换为字符串 "100.00"
		let parts = str.split('.'); // 按小数点分割
		let integerPart = parts[0]; // "100"
		let decimalPart = parts[1] || "00"; // "00"（如果没有小数部分，默认 "00"）
		if (!strr) {
			return integerPart
		} else {
			return decimalPart
		}

	}

	function gopage(id) {
		uni.navigateTo({
			url: '/pages/home/shangpdetail?id=' + id
		})
	}
	onMounted(() => {
		statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
		const {
			top,
			height
		} = wx.getMenuButtonBoundingClientRect();
		barHeight.value = height ? height + (top - statusBarHeight.value) * 2 : 38;
	})
</script>


<style lang="scss">
	.msgicon {
		width: 608rpx;
		margin: 100rpx auto;
		overflow: hidden;

		.msgtext {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #86909C;
			text-align: center;
			width: 100%;
			margin-top: -30rpx;
		}

		image {
			width: 608rpx;
		}
	}

	.fenlei {
		width: 100%;
		overflow: hidden;
		position: relative;

		.bottombox {
			width: 100%;
			margin-top: 20rpx;
			display: flex;
			overflow: hidden;

			.rightbox {
				width: 506rpx;
				margin-left: 20rpx;
				overflow-y: scroll;
				background: #FFFFFF;
				height: 1288rpx;

				.classifytitle {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #A4692D;
					margin-top: 24rpx;

				}

				.itemshangp {
					width: 100%;
					height: 202rpx;
					display: flex;
					align-items: center;
					border-bottom: 2rpx solid #F7F8FA;

					.leftimg {
						width: 146rpx;
						height: 146rpx;
						border-radius: 16rpx 16rpx 16rpx 16rpx;
					}

					.texts {
						width: 344rpx;
						overflow: hidden;
						margin-left: 20rpx;

						.t1 {
							white-space: nowrap;
							/* 防止文本换行 */
							overflow: hidden;
							/* 隐藏溢出的内容 */
							text-overflow: ellipsis;
							/* 显示省略号 */
							width: 100%;
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 24rpx;
							color: #1D2129;
						}

						.t2 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 22rpx;
							color: #4E5969;
							white-space: nowrap;
							/* 防止文本换行 */
							overflow: hidden;
							/* 隐藏溢出的内容 */
							text-overflow: ellipsis;
							/* 显示省略号 */
							width: 100%;
							margin-top: 14rpx;
						}

						.line {
							width: 100%;
							display: flex;
							align-items: center;
							justify-content: space-between;
							margin-top: 44rpx;

							.jiage {
								font-size: 28rpx;
								color: #F53F3F;
							}

							.righticon {
								width: 36rpx;
								height: 36rpx;
							}
						}
					}
				}
			}

			.leftmenubox {
				width: 192rpx;
				height: 1288rpx;
				background: #F2F3F5;
				border-radius: 0rpx 0rpx 0rpx 0rpx;

				.itemxq1 {
					width: 100%;
					height: 84rpx;
					text-align: center;
					line-height: 84rpx;
				}

				.itemxq {
					width: 180rpx;
					height: 84rpx;
					background: #FFFFFF;
					border-radius: 0rpx 20rpx 20rpx 0rpx;
					text-align: center;
					line-height: 84rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #A4692D;
					display: flex;
					align-items: center;
					text-align: center;

					.activebox {
						width: 6rpx;
						height: 50rpx;
						border-radius: 30rpx;
						margin-left: 8rpx;
						background: #A4692D;
						margin-right: 18rpx;
					}
				}
			}
		}

		.classify {
			width: 100%;
			margin-left: 32rpx;
			white-space: nowrap;
			margin-top: 192rpx;
			overflow-x: scroll;

			.itembox {
				display: inline-block;
				margin-right: 30rpx;

				.text {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					margin-top: 12rpx;
					height: 46.35rpx;
					width: 100%;
					text-align: center;
				}

				image {
					width: 104rpx;
					height: 104rpx;
					margin: auto;
					display: block;
				}

				.xz {
					padding: 0 20rpx;
					height: 46.35rpx;
					position: relative;
					margin-top: 12rpx;
					display: grid;

					image {
						width: 100% !important;
						height: 46.35rpx;
						position: absolute;
						z-index: -1;
					}

					.title {
						text-align: center;
						width: 100%;
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 26rpx;
						color: #FFFFFF;
						line-height: 43.35rpx;
					}
				}
			}
		}

		.gouwucheback {
			position: absolute;
			width: 100%;
			height: 430rpx;
			z-index: -1;
		}

		.headertop {
			width: 100%;
			position: fixed;
			top: 0%;
			overflow: hidden;
			transition: 0.5s;
			z-index: 9999;

			.box {
				width: 100%;
				display: flex;
				align-items: center;

				.pagelefticon {
					width: 152rpx;
					height: 40rpx;
					margin-left: 32rpx;
				}
			}
		}
	}
</style>